#pragma once
#include <memory>
#include <string>

class Any {
public:
    Any() = default;
    ~Any() = default;

    Any(const Any&) = delete;
    Any& operator=(const Any&) = delete;

    Any(Any&&) = default;
    Any& operator=(Any&&) = default;

    // 用于接收任意数据类型
    template <typename T>
    Any(T data) : base_(std::make_unique<Derive<T>>(data)) {
    }

    // 用于获取原本类型的数据
    template <typename T>
    T cast_() {
        Derive<T>* pd = dynamic_cast<Derive<T>*>(base_.get());
        if (!pd) throw std::string("mismatched type.");
        return pd->data_;
    }

private:
    class Base {
    public:
        virtual ~Base() = default;
    };

    template <typename T>
    class Derive : public Base {
    public:
        Derive(const T& data) : data_(data) {
        }

        T data_;
    };

private:
    std::unique_ptr<Base> base_;
};
